package com.barradata.common.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.barradata.R
import kotlinx.android.synthetic.main.plaintext_popup_fragment.*
import kotlinx.android.synthetic.main.plaintext_recyclerview_item.view.*

/**
 * Author:    WuNan
 * Email:     wunan@barradata.com
 * Created:   2019/7/8 16:52
 **/
class PlainTextPopupFragment(val contentList: List<String>) : BottomPopupFragment<Int>() {
    override val layoutId: Int = R.layout.plaintext_popup_fragment

    override fun initView() {
        val manager = LinearLayoutManager(this.context)
        recycler_view.layoutManager = manager
        recycler_view.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
                val v = LayoutInflater.from(parent.context).inflate(R.layout.plaintext_recyclerview_item, parent, false)
                return ViewHolder(v)
            }

            override fun getItemCount(): Int {
                return contentList.size
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                holder.itemView.title_text.text = contentList[position]
                holder.itemView.setOnClickListener {
                    callbackAction?.invoke(position)
                    dismiss()
                }
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}